﻿/* ----------------------------------------------------------------------------
 * Copyright (c) Philip Stears 
 *
 * This source code is subject to terms and conditions of the Microsoft Public 
 * License (Ms-PL).
 * 
 * By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 * 
 * To request a copy of the Microsoft Public License, send a request by email
 * to license@connectbasic.net.
 *
 * This notice must not be removed from this software.
 * ---------------------------------------------------------------------------- */

namespace ConnectBasic.Shell.Implementation.DefaultCodeEditor
{
    using System;
    using ConnectBasic.Shell.Extensibility;

    internal class CodeEditorCodeSnapshot : ICodeSnapshot
    {
        private string mBuffer;
        private IServiceProvider mLanguageServiceProvider;

        #region .ctor

        public CodeEditorCodeSnapshot(string buffer)
        {
            mBuffer = buffer;
        }

        #endregion

        #region ICodeSnapshot Members

        public string GetBufferContents()
        {
            return mBuffer;
        }

        #endregion

        #region IServiceProvider Members

        public object GetService(Type serviceType)
        {

            // If we've been handed a service provider by the consumer
            // of the snapshot, see if it has the requested service
            if (mLanguageServiceProvider != null)
            {
                var result = mLanguageServiceProvider.GetService(serviceType);

                if (result != null)
                {
                    return result;
                }
            }

            // We don't have any services of our own
            return null;
        }

        #endregion

        #region ICodeSnapshot Members

        public void AttachServices(IServiceProvider provider)
        {
            mLanguageServiceProvider = CombinedServiceProvider.Combine(mLanguageServiceProvider, provider);
        }

        #endregion

    }
}
